
import { FlowNodeTypeEnum } from "@/components/core/workflow/node/constant";
import { defaultApp } from "@/constants/defaultApp";
import { DebugDataType } from "@/enums/DebugDataType";
import { NodeOutputKeyEnum } from "@/enums/NodeOutputKeyEnum";
import { useMount, useRequest } from "ahooks";
import { useRouter } from "next/router";
import { createContext, useCallback, useState } from "react";
import { useEdgesState } from "reactflow";

export enum TabEnum {
    'appEdit' = 'appEdit',
    'publish' = 'publish',
    'logs' = 'logs'
}

export const AppDetailContext = createContext<any>({
    appId:'',
    action:TabEnum.appEdit,
    appDetail:defaultApp,
    loadingApp: false,
    setAppDetail:()=>{

    },
    updateAppDetail:()=>{

    },
    splitToolInputs: function (
        inputs: any[],
        nodeId: string
      ): {
        isTool: boolean;
        toolInputs: any[];
        commonInputs: any[];
      } {
        throw new Error('Function not implemented.');
      },

     
      
});

  /* If the module is connected by a tool, the tool input and the normal input are separated */

const AppContextProvider = ({children}:{children:any})=>{
    


  let initialNodes = [
    {
      id: "2",
      avatar:"https://fastgpt.in/logo.svg",
      data: {},
      outputs:[],
      name:"系统配置",
      nodeId:"2",
      intro:"可以配置应用的系统参数",
      inputs:[
        {
          key:"welcomeText",
          label:"core.app.Welcome Text",
          value:"你好，我是知识库助手",
          valueType: "string"
        }
      ],
      position: { x: -300, y: -100 },
      flowNodeType: "userGuide",
    },
    {
      id: "1",
      avatar:"https://fastgpt.in/logo.svg",
      name:'流程开始',
      position: { x: 100, y: 0 },
      nodeId:"workflowStartNodeId",
      outputs:[
        {
          id:"userChatInput",
          key:"userChatInput",
          label:"user chat output",
          type:"static",
        }
      ],
      inputs:[{
        key:"userChatInput",
        label:"用户问题",
        required:true,
        renderTypeList:['reference','textarea'],
        toolDescription:"用户问题",
        valueType:"string"
      }],
      flowNodeType: FlowNodeTypeEnum.workflowStart,
    },
    {
      nodeId:"dnVkjNcGTH05",
      avatar:"https://fastgpt.in/logo.svg",
      name:"指定回复1",
      flowNodeType: "answerNode",
      position: { x:500, y: 100 },
      outputs:[],
      inputs:[{
        renderTypeList:['reference','textarea'],
        description:"core.module.input.description.Response content",
        valueType:"any",
        key:"text",
        value:[
          "workflowStartNodeId","userChatInput"
        ]
      }],
      intro:"该模块可以直接回复一段指定的内容。常用于引导、提示。非字符串内容传入时，会转成字符串进行输出。"
    },
  
  ];



const edgesData = [
  {
    source:"workflowStartNodeId",
    sourceHandle:"workflowStartNodeId-source-right",
    target: "2",
    targetHandle: "2-target-left",
  }
];


    const [edges, setEdges, onEdgesChange] = useEdgesState([]);
    const router = useRouter()
    const {appId,action = TabEnum.appEdit} = router.query as {
        appId:string,
        action:TabEnum
    }
    
    const {loading} = useRequest(()=>{
        return Promise.resolve()
    },{
      onSuccess(){
        setAppDetail({
          ...appDetail,
          modules:initialNodes,
          edges:edgesData,
        })
      },
    })
    
    const [appDetail, setAppDetail] = useState<any>(defaultApp);

    const appDetailValue = {
        appId,
        appDetail,
        loadingApp:loading,
        action,
        setAppDetail
    }

    return (
    <AppDetailContext.Provider value={appDetailValue}>
        {children}
    </AppDetailContext.Provider>
    )
}

export default AppContextProvider;



